package br.com.futibamanager.dao;

import java.util.List;

import br.com.futibamanager.util.Constants;
import br.com.futibamanager.util.FacesUtil;

import com.db4o.ObjectContainer;

/**
 * An abstract class for DAO.
 * Any specific DAO must extend it.
 * 
 * @author diogenes.feijo
 *
 */
public abstract class DAO {

	/**
	 * Saves the object in the Object Container.
	 * 
	 * @param object - any object
	 */
	public <T extends Object> void save(T object) {
		
		if (object == null) {
			throw new IllegalArgumentException();
		}

		getObjectContainer().store(object);
	}
	
	/**
	 * Makes a query by example, based on object's state.
	 * 
	 * @param object - any object
	 * 
	 * @return List
	 */
	public <T> List<T> queryByExample(T object) {

		return getObjectContainer().queryByExample(object);
	}
	
	/**
	 * Obtains the Object Container.
	 * 
	 * @return ObjectContainer
	 */
	protected ObjectContainer getObjectContainer() {

		return (ObjectContainer) FacesUtil.getServletContext().getAttribute(Constants.DB4O_ATTRIBUTE);
	}

}
